<?php
namespace Menory\I18N;

class I18N {

    private $language = "zh-CN";
    private $resourcesFolderPath = __DIR__ . "/../resources/";

    public function _($content, $params = []) {
        $key = md5($content);
        $languageFilePath = sprintf("%s%s.php", $this->resourcesFolderPath, $this->language);
        $languages = [];
        if (file_exists($languageFilePath)) {
            $languages = include $languageFilePath;
        }
        if (isset($languages[$key])) {
            $content = $languages[$key];
        } else {
            $logFilePath = sprintf("%slog.php", $this->resourcesFolderPath);
            if (!file_exists($logFilePath)) {
                $logs = [$key => $content];
            } else {
                $logs = include $logFilePath;
                $logs[$key] = $content;
            }
            $str = "";
            foreach ($logs as $key => $value) {
                $str .= "\n\t\"{$key}\" => \"$value\",";
            }
            file_put_contents($logFilePath, "<?php\n\nreturn [{$str}\n];");
        }
        return $content;
    }

    /**
     * @return string$languages[$key]
     */
    public function getLanguage() {
        return $this->language;
    }

    /**
     * @param string $language
     */
    public function setLanguage($language) {
        $this->language = $language;
    }

}